/********************************************************************************
  * @file     SD93F115B_rtc.c
  * @author   TEST TEAM 												    	
  * @version  v0															    	
  * @date     Septemper 2022												    	
  * @brief
  *
  * Copyright (C) 2022  Hangzhou SDIC Microelectronics Co., Ltd
**********************************************************************************/
    
#include "SD93F115B_rtc.h"
#include "SD93F115B_rcc.h"

/*-------------------------------------------
 * 函数功能：将RTC相关寄存器恢复成上电默认值
 * 入口参数：None
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-------------------------------------------*/
void RTC_DeInit(void)
{
	RCC_APBPeriphResetCmd(RCC_APBPeriph_RTC, ENABLE);
    RCC_APBPeriphResetCmd(RCC_APBPeriph_RTC, DISABLE);
}

/*-----------------------------------------------------------
 * 函数功能：初始化RTC模块，主要是设置时间和日期，不包括闹钟
 * 入口参数：RTC_InitStruct: pointer to a RTC_InitStruct structure that
 *           contains the configuration information for the specified RTC peripheral.
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：None
 * 作者    ：
 *-----------------------------------------------------------*/
void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct)
{
	/* Check the parameters */
	assert_param(IS_RTC_Hour_Format(RTC_InitStruct->RTC_HourFormat));
	
	RTC_SetTime(RTC_InitStruct->RTC_HourFormat,&RTC_InitStruct->RTC_TimeStruct);
	RTC_SetDate(&RTC_InitStruct->RTC_DateStruct);
}

/*--------------------------------------------------------------------------
 * 函数功能：设置RTC时间
 * 入口参数：RTC_HourFormat,RTC_TimeStruct
 *			 RTC_HourFormat: 选择12小时制或24小时制 RTC_HourFormat_12,RTC_HourFormat_24
 *			 RTC_TimeStruct: pointer to a RTC_TimeStruct structure that
 *           contains the configuration information for the specified RTC Time peripheral.
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-------------------------------------------------------------------------*/ 
void RTC_SetTime(uint8_t RTC_HourFormat, RTC_TimeTypeDef* RTC_TimeStruct)
{
	uint32_t tmpreg=0;
	/* Check the parameters */
	assert_param(IS_RTC_Hour_Format(RTC_HourFormat));
	assert_param(IS_RTC_Minutes(RTC_TimeStruct->RTC_Minutes));
	assert_param(IS_RTC_Seconds(RTC_TimeStruct->RTC_Seconds));
	
	
		
	if (RTC_HourFormat == RTC_HourFormat_24)
	{
		assert_param(IS_RTC_Hour24(RTC_TimeStruct->RTC_Hours));	 	
		RTC->CR|=RTC_HourFormat_24;
	}
	else
	{
		assert_param(IS_RTC_Hour12(RTC_TimeStruct->RTC_Hours));
		RTC->CR&=~RTC_HourFormat_24;
	}
	tmpreg|=((uint32_t)(RTC_TimeStruct->RTC_Hours))<<16;	
	tmpreg|=((uint32_t)(RTC_TimeStruct->RTC_Minutes))<<8;
	tmpreg|=((uint32_t)(RTC_TimeStruct->RTC_Seconds));
	
	RTC->TR=tmpreg;	
}

/*--------------------------------------------------
 * 函数功能：获取RTC时间
 * 入口参数：RTC_HourFormat,RTC_TimeStruct
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *--------------------------------------------------*/ 
void RTC_GetTime(RTC_TimeTypeDef* RTC_TimeStruct)
{
	/* Fill the structure fields with the read parameters */
	RTC_TimeStruct->RTC_Hours = (uint8_t)((RTC->TR & 0x003f0000) >> 16);
	RTC_TimeStruct->RTC_Minutes = (uint8_t)((RTC->TR & 0x00007f00)>>8);
	RTC_TimeStruct->RTC_Seconds = (uint8_t)(RTC->TR & 0x0000007f);
	RTC_TimeStruct->RTC_H12 = RTC_TimeStruct->RTC_Hours>>5;
	
	RTC_TimeStruct->RTC_HourFormat=(uint8_t)(RTC->CR & 0x80);
}

/*------------------------------------------------
 * 函数功能：设置RTC日期
 * 入口参数：RTC_DateStruct: pointer to a RTC_DateStruct structure that
 *           contains the configuration information for the specified RTC Date peripheral.
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *------------------------------------------------*/ 
void RTC_SetDate(RTC_DateTypeDef* RTC_DateStruct)
{
	uint32_t tmpreg=0;
	/* Check the parameters */
	assert_param(IS_RTC_Year(RTC_DateStruct->RTC_Year));
    assert_param(IS_RTC_Month(RTC_DateStruct->RTC_Month));
    assert_param(IS_RTC_Date(RTC_DateStruct->RTC_Date));
//	assert_param(IS_RTC_Weekday(RTC_DateStruct->RTC_WeekDay));
    

	tmpreg|= (((uint32_t)RTC_DateStruct->RTC_Year)<<24);
	tmpreg|= (((uint32_t)RTC_DateStruct->RTC_Month)<<16);	
	tmpreg|= (((uint32_t)RTC_DateStruct->RTC_Date)<<8);
//	tmpreg|= (((uint32_t)RTC_DateStruct->RTC_WeekDay)<<8);
	
    RTC->DR=tmpreg;	
}

/*--------------------------------------------------------
 * 函数功能：获取RTC日期
 * 入口参数：RTC_DateStruct
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *--------------------------------------------------------*/ 
void RTC_GetDate(RTC_DateTypeDef* RTC_DateStruct)
{
	/* Fill the structure fields with the read parameters */
	RTC_DateStruct->RTC_Year  = (uint8_t)((RTC->DR & 0xff000000) >>24);
	RTC_DateStruct->RTC_Month = (uint8_t)((RTC->DR & 0x001f0000) >>16);
	RTC_DateStruct->RTC_Date=(uint8_t)((RTC->DR & 0x00003f00) >>8);	
	RTC_DateStruct->RTC_WeekDay = (uint8_t)(RTC->DR & 0x00000003);	
}

/*----------------------------------------
 * 函数功能：RTC使能
 * 入口参数：NewState: ENABLE或者DISABLE
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------*/
void RTC_Cmd(FunctionalState NewState)
{
	/* Check the parameters */;
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	
	if (NewState != DISABLE)
	{

		RTC->CR |= RTC_CR_RTCEN;
	}
	else
	{
		RTC->CR &= ~RTC_CR_RTCEN;
	}		
}

/*---------------------------------------------------------------------------
 * 函数功能：设置RTC闹钟
 * 入口参数：RTC_HourFormat,RTC_AlarmStruct
 *			 RTC_HourFormat: RTC_HourFormat_24,RTC_HourFormat_12
 *			 RTC_AlarmStruct: pointer to a RTC_AlarmStruct structure that
 *           contains the configuration information for the specified RTC Alarm peripheral.
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *--------------------------------------------------------------------------*/ 
void RTC_SetAlarm(uint8_t RTC_HourFormat,RTC_AlarmTypeDef* RTC_AlarmStruct)
{
	uint32_t tmpreg=0;
	/* Check the parameters */
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	assert_param(IS_RTC_Alarm_Date_WeekDay_SEL(RTC_AlarmStruct->RTC_AlarmDateOrWeekDaySel));
	assert_param(IS_RTC_Hour_Format(RTC_HourFormat));
	
	
	
	//匹配日期或星期
	if(RTC_AlarmStruct->RTC_AlarmMaskDateOrWeekDay==DISABLE)
	{
		tmpreg&=~RTC_ALARMR_MSK4;
		if(RTC_AlarmStruct->RTC_AlarmDateOrWeekDaySel==RTC_AlarmDateOrWeekDaySel_Date)
		{
			assert_param(IS_RTC_Date(RTC_AlarmStruct->RTC_DateOrWeekDay));
		}
		else
		{
			assert_param(IS_RTC_Weekday(RTC_AlarmStruct->RTC_DateOrWeekDay));
		}
		tmpreg|=((uint32_t)(RTC_AlarmStruct->RTC_AlarmDateOrWeekDaySel|RTC_AlarmStruct->RTC_DateOrWeekDay))<<24;

	}
	else
		tmpreg|=RTC_ALARMR_MSK4; 
		
	//匹配小时
	if(RTC_AlarmStruct->RTC_AlarmMaskHours==DISABLE)
	{
		tmpreg&=~RTC_ALARMR_MSK3;
		if (RTC_HourFormat == RTC_HourFormat_24)
		{
			tmpreg|=RTC_ALARMR_A12_24;
			
			assert_param(IS_RTC_Hour24(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));	 	
	    }
		else
		{
			tmpreg &=~RTC_ALARMR_A12_24;
			assert_param(IS_RTC_Hour12(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
	    }
		tmpreg|=((uint32_t)RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)<<16;
	}
	else
		tmpreg|=RTC_ALARMR_MSK3;

	//匹配分钟		
	if(RTC_AlarmStruct->RTC_AlarmMaskMinutes==DISABLE)
	{
		tmpreg&=~RTC_ALARMR_MSK2;
		tmpreg|=((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes))<<8;

	}
	else
		tmpreg|=RTC_ALARMR_MSK2;
 	
	//匹配秒		
	if(RTC_AlarmStruct->RTC_AlarmMaskSeconds==DISABLE)
	{
		tmpreg&=~RTC_ALARMR_MSK1; 
		tmpreg|=((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds));
	}
	else
		tmpreg|=RTC_ALARMR_MSK1;

	RTC->ALARMR=tmpreg; 		
}

/*---------------------------------------------
 * 函数功能：使能RTC闹钟功能
 * 入口参数：NewState: ENABLE或者DISABLE
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------------*/
void RTC_AlarmCmd(FunctionalState NewState)
{
	/* Check the parameters */;
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	
	if (NewState != DISABLE)
	{

		RTC->CR |= RTC_CR_TAEN;
	}
	else
	{
		RTC->CR &= ~RTC_CR_TAEN;
	}
}
/*-----------------------------
 * 函数功能：RTC中断配置
 * 入口参数：RTC_IT,NewState
 *			 RTC_IT: RTC_IT_RTCIE,RTC_IT_TAIE
 *           NewState: ENABLE或者DISABLE
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *------------------------------*/ 
void RTC_ITConfig(uint8_t RTC_IT, FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_RTC_ITSource(RTC_IT));
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	
	if (NewState != DISABLE)
	{
		RTC->CR |= RTC_IT;
	}
	else
	{
		RTC->CR &= ~RTC_IT;
	} 
}

/*-----------------------------
 * 函数功能：读RTC标志位状态
 * 入口参数：RTC_IT: RTC_IT_RTCIE,RTC_IT_TAIE
 * 出口参数：None
 * 返回值  ：RTC标志位状态bitstatus
 * 编写日期：
 * 作者    ：
 *------------------------------*/
ITStatus RTC_GetITStatus(uint8_t RTC_IT)
{
	/* Check the parameters */
	assert_param(IS_RTC_ITSource(RTC_IT));
	
	FlagStatus bitstatus = RESET;

	if ((RTC->SR & RTC_IT) != RESET)
	{
		bitstatus = SET;
	}
	else
	{
		bitstatus = RESET;
	}
	return  bitstatus;		
}

/*-----------------------------
 * 函数功能：清零RTC标志位
 * 入口参数：RTC_IT: RTC_IT_RTCIE,RTC_IT_TAIE
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *------------------------------*/
void RTC_ClearITStatus(uint8_t RTC_IT)
{
	/* Check the parameters */
	assert_param(IS_RTC_ITSource(RTC_IT));	
	RTC->SR |= RTC_IT;
	
}


/****************************END OF FILE****************************/